package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.pojo.Employee;
import com.itheima.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author Gareth Cao
 * @date 2022/4/25 16:46
 */
@RestController
@RequestMapping("/employee")
@Slf4j
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 用户登录
     *
     * @param employee
     */
    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee, HttpServletRequest request) {
        //获取用户姓名
        String username = employee.getUsername();
        //根据姓名查询数据库
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        Employee emp = employeeService.getOne(queryWrapper);
        //判断用户名是否存在
        if (emp == null) {
            //类名调静态方法
            return R.error("用户名不存在");
        }

        //获取数据库中该用户密码
        String pw1 = emp.getPassword();
        //根据加密算法把登录的密码换成加密之后的字符串
        String pw2 = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
        if (!pw1.equals(pw2)) {
            return R.error("密码错误");
        }

        //从数据库获取用户的状态
        Integer status = emp.getStatus();
        if (status != 1) {
            return R.error("用户禁用");
        }
        //将查询到的用户id存到session中,这样可以确定一次会话中是存在用户的
        request.getSession().setAttribute("empId", emp.getId());
        return R.success(emp);
    }

    /**
     * 退出登录界面功能
     *
     * @param session
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(HttpSession session) {
        //退出之后销毁session
        session.invalidate();
        return R.success("成功退出");
    }

    /**
     * 新增员工功能
     *
     * @param employee
     * @param session
     * @return
     */
    @PostMapping
    public R<Employee> add(@RequestBody Employee employee, HttpSession session) {
        //浏览器发来的数据不全,需要补全
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        //employee.setCreateTime(LocalDateTime.now());
        //employee.setUpdateTime(LocalDateTime.now());
        //employee.setCreateUser((Long) session.getAttribute("empId"));
        //employee.setUpdateUser((Long) session.getAttribute("empId"));
        //将数据存到数据库
        employeeService.save(employee);
        //如果新增有异常则不执行下列语句
        return R.success(employee);
    }

    /**
     * 带条件的分页查询
     *
     * @param name
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page<Employee>> selectByPage(String name, Integer page, Integer pageSize) {
        Page<Employee> pageInfo = new Page<>(page, pageSize);
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(name), "name", name);
        //底层会将带条件的分页查询结果封装到pageInfo里面去
        employeeService.page(pageInfo, queryWrapper);
        return R.success(pageInfo);
    }

    /**
     * 对员工进行禁用启用操作
     * 下列方法还可以满足编辑时的保存操作,方法是复用的,保存也是要mployee employee, HttpSession session这两个数据
     * @param employee
     * @param session
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Employee employee, HttpSession session) {
        //更新修改时间
        //employee.setUpdateTime(LocalDateTime.now());
        //更新的用户
        //employee.setUpdateUser((Long) session.getAttribute("empId"));
        //打印当前线程的id
        log.info("当前线程的id: {}",Thread.currentThread().getId());
        employeeService.updateById(employee);
        return R.success("修改成功");
    }

    /**
     * 根据id查询进行数据回显的操作
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> findById(@PathVariable("id") Long id){
        Employee employee = employeeService.getById(id);
        return R.success(employee);
    }
}
